package com.bub.pay.outer.redis.redis;

import cn.jpush.api.JPushClient;
import com.bub.pay.framework.common.async.ExceptionHandlingAsyncTaskExecutor;
import com.bub.pay.framework.common.template.JPushTemplate;
import com.bub.pay.framework.common.template.JPushVo;
import com.bub.pay.framework.redis.RedisClient;
import com.bub.pay.framework.redis.model.JPushModel;
import com.bub.pay.outer.redis.AsyncJPushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

public class RedisJPushListener extends MessageListenerAdapter {
    /** Logger */
    private static final Logger logger = LoggerFactory.getLogger(RedisHttpListener.class);

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private JPushClient jPushClient;

    @Autowired
    private ExceptionHandlingAsyncTaskExecutor taskExecutor;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        if (null == message) {
            return;
        }
        Object obj = redisClient.getRedisTemplate().getDefaultSerializer().deserialize(message.getBody());
        if (obj instanceof JPushModel) {
            final JPushModel model = (JPushModel) obj;
            JPushVo jPushVo = JPushTemplate.getContent(model.getTemplateId(),model.getData(),model.getRegistrationId(),model.isProd());
            if (model != null) {
                logger.info("极光推送,title:"+jPushVo.getTitle());
                taskExecutor.execute(new AsyncJPushService(jPushClient, jPushVo));
            }

        }
    }
}
